import { observe } from "./observer/index.js";
export function initState(vm) {
  const opts = vm.$options;
  console.log("initState", this);
  if (opts.props) {
    initProps(vm);
  }
  if (opts.methods) {
    initMethods(vm);
  }
  if (opts.data) {
    initData(vm);
  }
  if (opts.computed) {
    initComputed(vm);
  }
  if (opts.watch) {
    initWatch(vm);
  }
}
function initProps(vm) {
  const props = vm.$options.props;
  if (props) {
    for (let key in props) {
      console.log("key", key);
      // 初始化props
      defineReactive(vm, key, props[key]);
    }
  }
}
function initMethods(vm) {
  const methods = vm.$options.methods;
  if (methods) {
    for (let key in methods) {
      vm[key] = methods[key];
    }
  }
}
function initData(vm) {
  let data = vm.$options.data;
  //   console.log("🚀 ~ initData ~ data:", data);
  data = vm._data = typeof data === "function" ? data.call(vm) : data;
  // 对象劫持
  observe(data);
  //   console.log(this, vm);
  //   if (typeof data === "function") {
  //     // 获取data
  //     data = data.call(vm);
  //   }
  //   for (let key in data) {
  //     defineReactive(vm, key, data[key]);
  //   }
}
function initComputed(vm) {
  const computed = vm.$options.computed;
  if (computed) {
    for (let key in computed) {
      defineComputed(vm, key);
    }
  }
}
function initWatch(vm) {
  const watch = vm.$options.watch;
  if (watch) {
    for (let key in watch) {
      createWatcher(vm, key);
    }
  }
}
